﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Yipusoft.Common.BLL;
using System.Text;
using Yipusoft.Evaluate.BLL;
using Yipusoft.Evaluate.Model;
using Yipusoft.Task.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using SmartWeb.BLL;

public partial class Evaluate_AllAudit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDate();
            init();
        }
    }

    private int dcount;
    private int tlength = 10;
    private decimal temp = 0;
    private decimal[] total = new decimal[100];

    void BindDate()
    {
        DateTime now = DateTime.Now.AddMonths(-1);
        for (int i = 2009; i <= 2050; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString() + "年", i.ToString()));
        }
        ddlYear.SelectedValue = now.Year.ToString();
        if (Request["year"] != null)
        {
            ddlYear.SelectedValue = Request["Year"];
        }
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        ddlMonth.SelectedValue = now.Month.ToString();
        if (Request["month"] != null)
        {
            ddlMonth.SelectedValue = Request["month"];
        }
    }

    void init()
    {

        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(Page.User.Identity.Name).DepartmentID;
        if (DepartmentId.Substring(0, 4) == "1300" && DepartmentId.Substring(6, 1) == "0")
        {//参谋长

            string key = DepartmentId.Substring(5, 1);
            BindData(key);

        }
        else
        {
            BindData();
        }
    }

    #region 参谋长查看
    void BindData(string key)
    {

        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);

        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'and left(DepartmentID,4)<>'1314' And Left(DepartmentID,4)<>'1315'").Tables[0].DefaultView;
        dcount = dv.Count;
        for (int i = 0; i < dcount; i++)
        {
            total[i] = 0;
        }
        TableRow tr = new TableRow();
        tr.CssClass = "grid_header";
        tr.HorizontalAlign = HorizontalAlign.Right;
        //月份
        TableCell tc = new TableCell();
        tc.CssClass = "Td";
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.Text = "项目";
        tc.ColumnSpan = 2;
        tr.Cells.Add(tc);

        foreach (DataRowView drv in dv)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Width = Unit.Pixel(40);
            tc.Text = drv["AliasName"].ToString();
            tr.Cells.Add(tc);
        }

        tb.Rows.Add(tr);

        List<DepartmentInfo> UnitList = GetUnit(key);
        foreach (DepartmentInfo Unit in UnitList)
        {
            TableCell headCell = new TableCell();
            headCell.CssClass = "Td";
            headCell.HorizontalAlign = HorizontalAlign.Left;
//            headCell.Text = Unit.ShortName.Substring(5);
	    headCell.Text = Unit.ShortName.Substring(Unit.ShortName.Length > 5 ? 5 : Unit.ShortName.Length);
            int ros2 = 0;

            ros2 = BindTaskTable(dcount, Unit.DepartmentID, ros2, headCell);
            ros2 = BindBaseTable(dcount, Unit.DepartmentID, ros2, headCell);
            headCell.RowSpan = ros2;



        }
        int row = 0;
        row = BindInspectTable(row, dcount);
        row = BindCheckTable(row, dcount);
        row = BindAwardTable(row, dcount);
        BindTotalTable();
        BindChart();

        //审核意见
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string where = "year=" + year + "  and month=" + month + " and DepartmentID like '" + DepartmentId.Substring(0, 6) + "%' order by SetDate";
        List<AuditInfo> list = Audit.GetList(where);
        dlOpinion.DataSource = list;
        dlOpinion.DataBind();
        if (list.Count == 0)
        {
            pnl.Visible = false;
        }
        else
        {
            pnl.Visible = true;
        }
    }

    private int BindTaskTable(int dcount, string DepartmentId, int ros2, TableCell headCell)
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN UserId = ''' + DepartmentId + ''' THEN Score END) AS D' + DepartmentId FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,1) = '0' AND SUBSTRING(DepartmentId,6,1) = '" + DepartmentId.Substring(5, 1) + "' AND SUBSTRING(DepartmentId,7,1) = '0' And left(DepartmentID,4)<>'1314' And Left(DepartmentID,4)<>'1315'");
        sql.AppendLine("SET @SQL = 'SELECT TaskId ' +@SQL + ' FROM TaskUsers WHERE TaskId IN (SELECT TaskId FROM Tasks WHERE CreateDepartment=''" + DepartmentId + "'' And YEAR(EndDate) =" + year + " AND MONTH(EndDate)=" + month + ") GROUP BY TaskId'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            TableRow tr = new TableRow();
            ros2++;
            if (ros2 == 1)
            {
                tr.Cells.Add(headCell);
            }
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Left;
            title = ros2.ToString() + "、" + Task.GetTask((int)drv[0]).Title;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);


            for (int i = 1; i <= dcount; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.HorizontalAlign = HorizontalAlign.Right;
                tc.Width = Unit.Pixel(40);
                string RootId = drv[0].ToString();

                string UserId = dv.Table.Columns[i].ColumnName.Replace("D", "");
                if (drv[i] != DBNull.Value)
                {
                    string Text = decimal.Parse(drv[i].ToString()).ToString("0");
                    tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">" + Text + "</a>";
                }
                else
                {

                    tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">未审核</a>";
                }
                tr.Cells.Add(tc);
                if (drv[i] != DBNull.Value)
                {
                    total[i - 1] += (decimal)drv[i];
                }

            }
            tb.Rows.Add(tr);

        }

        return ros2;
    }


    private int BindBaseTable(int dcount, string DepartmentId, int ros2, TableCell headCell)
    {

        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        string strSql = "select * from Base where UnitId='" + DepartmentId + "' ";
        DataTable baseDt = SplitPage.ExecuteSQL(strSql).Tables[0];
        foreach (DataRow dr in baseDt.Rows)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("DECLARE @SQL nvarchar(4000)");
            sql.AppendLine("SET @SQL = ''");
            sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000' and left(DepartmentID,4)<>'1314' And Left(DepartmentID,4)<>'1315'");
            sql.AppendLine("SET @SQL = 'SELECT a.BaseID, a.BaseName' + @SQL + ' FROM Base a LEFT JOIN BaseItems b ON a.BaseID = b.BaseID WHERE (b.YearValue=" + year + " and b.MonthValue=" + month + ") and a.BaseID in (" + dr["BaseID"] + ")  GROUP BY a.BaseID, a.BaseName'");
            sql.AppendLine("EXEC(@SQL)");
            DataRow drv = null;
            DataTable dt = SplitPage.ExecuteSQL(sql.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                drv = dt.Rows[0];
            }
            TableRow tr = new TableRow();

            tr.HorizontalAlign = HorizontalAlign.Right;
            ros2++;
            if (ros2 == 1)
            {
                tr.Cells.Add(headCell);
            }
            TableCell tc = new TableCell();

            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = ros2.ToString() + "、" + Base.GetBase(Convert.ToInt32(dr["BaseID"])).BaseName;
            tr.Cells.Add(tc);

            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv != null && drv[i] != DBNull.Value)
                    temp = Base.GetBase(Convert.ToInt32(dr["BaseID"])).TotalScore - (decimal)drv[i];
                else
                    temp = Base.GetBase(Convert.ToInt32(dr["BaseID"])).TotalScore;
                total[i - 2] += temp;
                tc.Text = "";
                tc.Text = "<a href=BaseView2.aspx?BaseId=" + dr["BaseID"] + "&DepartmentId=" + dt.Columns[i].ColumnName.Replace("D", "") + "&Year=" + year + "&month=" + month + "&EndMonth=" + month + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
        }
        return ros2;

    }
    #endregion

    #region
    void BindData()
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);


        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'and left(DepartmentID,4)<>'1314'  And Left(DepartmentID,4)<>'1315'").Tables[0].DefaultView;
        dcount = dv.Count;
        for (int i = 0; i < dcount; i++)
        {
            total[i] = 0;
        }
        TableRow tr = new TableRow();
        tr.CssClass = "grid_header";
        tr.HorizontalAlign = HorizontalAlign.Right;
        //月份
        TableCell tc = new TableCell();
        tc.CssClass = "Td";
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.Text = "项目";
        tc.ColumnSpan = 2;
        tr.Cells.Add(tc);
        foreach (DataRowView drv in dv)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Width = Unit.Pixel(40);
            tc.Text = drv["AliasName"].ToString();
            tr.Cells.Add(tc);
        }

        tb.Rows.Add(tr);
        int row = 0;
        //工作任务
        row = BindTaskTable(row, dcount);
        //基础数据
        row = BindBaseTable(row, dcount);
        //
        row = BindInspectTable(row, dcount);
        //
        row = BindCheckTable(row, dcount);
        //
        row = BindAwardTable(row, dcount);
        BindTotalTable();
        BindChart();
        //审核意见
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        string where = "year=" + year + "  and month=" + month + " and (DepartmentID='" + DepartmentId + "' or DepartmentID='" + DepartmentId.Substring(0, 6) + "0') order by SetDate";
        List<AuditInfo> list = Audit.GetList(where);
        dlOpinion.DataSource = list;
        dlOpinion.DataBind();
        if (list.Count == 0)
        {
            pnl.Visible = false;
        }
        else
        {
            pnl.Visible = true;
        }
    }



    private int BindTaskTable(int ros, int dcount)
    {
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(Page.User.Identity.Name).DepartmentID;

        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN UserId = ''' + DepartmentId + ''' THEN Score END) AS D' + DepartmentId FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,1) = '0' AND SUBSTRING(DepartmentId,6,1) = '" + DepartmentId.Substring(5, 1) + "' AND SUBSTRING(DepartmentId,7,1) = '0'  and left(DepartmentID,4)<>'1314' and left(DepartmentID,4)<>'1315'");
        sql.AppendLine("SET @SQL = 'SELECT TaskId ' +@SQL + ' FROM TaskUsers WHERE TaskId IN (SELECT TaskId FROM Tasks WHERE CreateDepartment=''" + DepartmentId + "'' And YEAR(EndDate) =" + year + " AND MONTH(EndDate)=" + month + ") GROUP BY TaskId'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            ros++;
            TableRow tr = new TableRow();
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.ColumnSpan = 2;
            tc.HorizontalAlign = HorizontalAlign.Left;
            title = ros.ToString() + "、" + Task.GetTask((int)drv[0]).Title;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);


            for (int i = 1; i <= dcount; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.HorizontalAlign = HorizontalAlign.Right;
                tc.Width = Unit.Pixel(40);
                string RootId = drv[0].ToString();

                string UserId = dv.Table.Columns[i].ColumnName.Replace("D", "");
                if (drv[i] != DBNull.Value)
                {
                    string Text = decimal.Parse(drv[i].ToString()).ToString("0");
                    tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">" + Text + "</a>";
                }
                else
                {

                    tc.Text = "<a href=TaskView.aspx?RootId=" + RootId + "&UserId=" + UserId + ">未审核</a>";
                }
                tr.Cells.Add(tc);
                if (drv[i] != DBNull.Value)
                {
                    total[i - 1] += (decimal)drv[i];
                }

            }
            tb.Rows.Add(tr);

        }

        return ros;
    }


    private int BindBaseTable(int ros, int dcount)
    {
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(Page.User.Identity.Name).DepartmentID;
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);


        string strSql = "select * from Base where UnitId='" + DepartmentId + "' ";
        DataTable baseDt = SplitPage.ExecuteSQL(strSql).Tables[0];
        foreach (DataRow dr in baseDt.Rows)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("DECLARE @SQL nvarchar(4000)");
            sql.AppendLine("SET @SQL = ''");
            sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '000' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'");
            sql.AppendLine("SET @SQL = 'SELECT a.BaseID, a.BaseName' + @SQL + ' FROM Base a LEFT JOIN BaseItems b ON a.BaseID = b.BaseID WHERE (b.YearValue=" + year + " and b.MonthValue=" + month + ") and a.BaseID in (" + dr["BaseID"] + ")  GROUP BY a.BaseID, a.BaseName'");
            sql.AppendLine("EXEC(@SQL)");
            DataRow drv = null;
            DataTable dt = SplitPage.ExecuteSQL(sql.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {
                drv = dt.Rows[0];
            }
            TableRow tr = new TableRow();

            tr.HorizontalAlign = HorizontalAlign.Right;
            TableCell tc = new TableCell();
            ros++;
            tc.CssClass = "Td";
            tc.ColumnSpan = 2;
            tc.HorizontalAlign = HorizontalAlign.Left;
            tc.Text = ros.ToString() + "、" + Base.GetBase(Convert.ToInt32(dr["BaseID"])).BaseName;
            tr.Cells.Add(tc);

            for (int i = 2; i <= dcount + 1; i++)
            {


                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv != null && drv[i] != DBNull.Value)
                    temp = Base.GetBase(Convert.ToInt32(dr["BaseID"])).TotalScore - (decimal)drv[i];
                else
                    temp = Base.GetBase(Convert.ToInt32(dr["BaseID"])).TotalScore;
                total[i - 2] += temp;
                tc.Text = "";
                tc.Text = "<a href=BaseView2.aspx?BaseId=" + dr["BaseID"] + "&DepartmentId=" + dt.Columns[i].ColumnName.Replace("D", "") + "&Year=" + year + "&month=" + month + "&EndMonth=" + month + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
        }
        return ros;

    }



    private int BindInspectTable(int ros, int dcount)
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
	if(departmentid=="1300012")
		return ros;
        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '0" + departmentid.Substring(5, 1) + "0' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'");
        sql.AppendLine("SET @SQL = 'SELECT a.InspectID, a.InspectName' + @SQL + ' FROM Inspects a LEFT JOIN InspectItems b ON a.InspectID = b.InspectID WHERE YEAR(a.InspectDate)=" + year + " AND MONTH(a.InspectDate)=" + month
 + " and  a.departmentId=''" + departmentid.Substring(0, 6) + "0'' GROUP BY a.InspectID, a.InspectName, a.InspectDate ORDER BY a.InspectDate'");
        sql.AppendLine("EXEC(@SQL)");	
	
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableRow tr;
        TableCell tc;
        string title = "";

        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();

            ros++;
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.ColumnSpan = 2;
            tc.BackColor = System.Drawing.Color.White;
            title = ros.ToString() + "、" + Inspect.GetInspect((int)drv[0]).InspectName;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);
            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv[i] != DBNull.Value)
                    temp = Inspect.GetInspect((int)drv[0]).TotalScore - (decimal)drv[i];
                else
                    temp = Inspect.GetInspect((int)drv[0]).TotalScore;
                total[i - 2] += temp;
                tc.Text = "<a href=InspectView.aspx?InspectId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);
        }
        return ros;
    }

    private int BindCheckTable(int ros, int dcount)
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '0" + departmentid.Substring(5, 1) + "0' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'");
        sql.AppendLine("SET @SQL = 'SELECT a.CheckID, a.CheckName' + @SQL + ' FROM Checks a LEFT JOIN CheckItems b ON a.CheckID = b.CheckID WHERE YEAR(a.CheckDate)=" + year + " AND MONTH(a.CheckDate)=" + month + " and a.departmentId=''" + departmentid.Substring(0, 6) + "0'' GROUP BY a.CheckID, a.CheckName, a.CheckDate ORDER BY a.CheckDate'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableRow tr;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.ColumnSpan = 2;
            tc.BackColor = System.Drawing.Color.White;
            ros++;
            title = ros.ToString() + "、" + Check.GetCheck((int)drv[0]).CheckName;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);
            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv[i] != DBNull.Value)
                    temp = Check.GetCheck((int)drv[0]).TotalScore - (decimal)drv[i];
                else
                    temp = Check.GetCheck((int)drv[0]).TotalScore;
                total[i - 2] += temp;
                tc.Text = "<a href=CheckView.aspx?CheckId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);

        }
        return ros;
    }

    private int BindAwardTable(int ros, int dcount)
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;

        StringBuilder sql = new StringBuilder();
        sql.AppendLine("DECLARE @SQL nvarchar(4000)");
        sql.AppendLine("SET @SQL = ''");
        sql.AppendLine("SELECT @SQL = @SQL + ', SUM(CASE WHEN LEFT(b.DepartmentId,4) = ''' + LEFT(DepartmentId,4) + ''' THEN b.ItemScore END) AS D' + LEFT(DepartmentId,4) + '0" + departmentid.Substring(5, 1) + "0' FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000'");
        sql.AppendLine("SET @SQL = 'SELECT a.AwardID, a.AwardName' + @SQL + ' FROM Awards a LEFT JOIN AwardItems b ON a.AwardID = b.AwardID WHERE YEAR(a.AwardDate)=" + year + " AND MONTH(a.AwardDate)=" + month + " and a.departmentId=''" + departmentid.Substring(0, 6) + "0'' GROUP BY a.AwardID, a.AwardName, a.AwardDate ORDER BY a.AwardDate'");
        sql.AppendLine("EXEC(@SQL)");
        DataView dv = SplitPage.ExecuteSQL(sql.ToString()).Tables[0].DefaultView;
        TableRow tr;
        TableCell tc;
        string title = "";
        foreach (DataRowView drv in dv)
        {
            tr = new TableRow();
            tr.CssClass = "grid_item";

            ros++;

            tc = new TableCell();
            tc.CssClass = "Td";
            tc.ColumnSpan = 2;
            tc.BackColor = System.Drawing.Color.White;
            title = ros.ToString() + "、" + Award.GetAward((int)drv[0]).AwardName;
            if (title.Length > tlength)
                tc.Text = title.Substring(0, tlength) + "...";
            else
                tc.Text = title;
            tr.Cells.Add(tc);
            for (int i = 2; i <= dcount + 1; i++)
            {
                tc = new TableCell();
                tc.CssClass = "Td";
                tc.BackColor = System.Drawing.Color.White;
                tc.HorizontalAlign = HorizontalAlign.Right;
                if (drv[i] != DBNull.Value)
                    temp = (decimal)drv[i];
                else
                    temp = 0;
                total[i - 2] += temp;
                tc.Text = "<a href=AwardView.aspx?AwardId=" + drv[0].ToString() + "&DepartmentId=" + dv.Table.Columns[i].ColumnName.Replace("D", "") + ">" + temp.ToString("0") + "</a>";
                tr.Cells.Add(tc);
            }
            tb.Rows.Add(tr);

        }
        return ros;
    }
    private void BindTotalTable()
    {

        TableRow tr = new TableRow();
        tr.ID = Guid.NewGuid() + "tr";
        tr.CssClass = "grid_item";
        TableCell tc = new TableCell();
        TableCell tc1 = new TableCell();
        tc1.CssClass = "Td";
        tc1.ColumnSpan = 2;
        tc1.ID = Guid.NewGuid() + "td";
        tc1.Text = "合计";


        tr.Cells.Add(tc1);
        for (int i = 0; i < dcount; i++)
        {
            tc = new TableCell();
            tc.CssClass = "Td";
            tc.ID = Guid.NewGuid() + "td";
            tc.HorizontalAlign = HorizontalAlign.Right;
            tc.Text = total[i].ToString("0");
            tr.Cells.Add(tc);
        }

        tb.Rows.AddAt(tb.Rows.Count, tr);
        tc1.RowSpan = 1;
    }
    private void BindChart()
    {
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);

        List<ResultInfo> results = new List<ResultInfo>();
        ResultInfo result = null;
        DataView dv = SplitPage.ExecuteSQL("SELECT DepartmentID, AliasName FROM Account_Departments WHERE SUBSTRING(DepartmentId,3,2) <> '00' AND SUBSTRING(DepartmentId,5,3) = '000' And Left(DepartmentID,4)<>'1315' And Left(DepartmentID,4)<>'1314'").Tables[0].DefaultView;
        for (int i = 0; i < dv.Count; i++)
        {
            result = new ResultInfo();
            result.DepartmentName = dv[i]["AliasName"].ToString();

            if (total[i] > 0)
            {
                result.Score = total[i];
            }
            else
                result.Score = 0;

            results.Add(result);
        }
        Comparer c = new Comparer();
        results.Sort(c);
        UserChart.Titles["Title"].Text = year + "年" + month + "月各单位综合得分统计表";
        UserChart.ChartAreas["Default"].AxisX.LabelsAutoFit = false;

        
        UserChart.Series["综合得分"].Points.DataBindXY(results, "DepartmentName", results, "Score");
    }

    //获取各处室
    public List<DepartmentInfo> GetUnit(string DepartKey)
    {
        List<DepartmentInfo> list = new List<DepartmentInfo>();
        string where = " DepartmentId like '13000" + DepartKey + "%' ";
        if (DepartKey == "1")
        {
            list.Add(Department.GetDepartment("1300013"));
            list.Add(Department.GetDepartment("1300012"));
            list.Add(Department.GetDepartment("1300014"));
            list.Add(Department.GetDepartment("1300011"));
            list.Add(Department.GetDepartment("1300019"));
        }
        else
        {
            where = " DepartmentId like '13000" + DepartKey + "%' ";
            list = Department.GetList(where);
        }

        return list;
    }

    #endregion
    //查看
    protected void lkbView_Click(object sender, EventArgs e)
    {
        Response.Redirect("AllAudit.aspx?Year=" + ddlYear.SelectedValue + "&month=" + ddlMonth.SelectedValue);
    }
    //审核
    protected void Button1_Click(object sender, EventArgs e)
    {
        //if (rblDuty.SelectedValue == "")
        //{
        //    Prompt.Text = "提示：请选择职务！";
        //    return;
        //}
        //if (txtRemark.Text.Trim() == "")
        //{
        //    Prompt.Text = "提示：意见不能为空！";
        //    return;
        //}
        int year = Convert.ToInt32(ddlYear.SelectedValue);
        int month = Convert.ToInt32(ddlMonth.SelectedValue);
        string departmentid = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        AuditInfo audit = new AuditInfo();
        audit.DepartmentID = departmentid;
        audit.Duty = rblDuty.SelectedValue;
        audit.Id = Guid.NewGuid();
        audit.Month = month;
        audit.Year = year;
        audit.Remark = SmartWeb.BLL.UserFun.TextToHtml(txtRemark.Text);
        audit.SetDate = DateTime.Now;
        audit.UserId = User.Identity.Name;
        audit.UserName = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName;
        try
        {
            Guid id = Audit.Insert(audit);
            if (id != Guid.Empty)
            {
                Response.Redirect("AllAudit.aspx?Year=" + ddlYear.SelectedValue + "&month=" + ddlMonth.SelectedValue);
            }
        }
        catch (Exception ex)
        {
            Prompt.Text = "提示：" + ex.Message;
        }
    }
}
